package com.ideal.timer;

import com.ideal.Interface.MainConfig;
import com.ideal.Interface.HttpSend;
import com.ideal.Interface.DevProcess;
import com.ideal.log.Log;
import java.util.TimerTask;
import com.ideal.Interface.DevInterface;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;

public class GetInfoTimer extends TimerTask {

    private boolean isRunning = false;
    private DevProcess pro = null;//设备接口
    private String mKeyName = "";//报文关键字

    public GetInfoTimer(DevInterface dev,String KeyName) {
        Log.logger.info("Create new device process");
        pro = new DevProcess();
        Log.logger.info("Register new device");
        pro.RegisterDev(dev);//注册设备bcmc
        mKeyName = KeyName;
    }

    public void run() {
        if (!isRunning) {
            isRunning = true;
            Log.logger.info("start Timer task ");

            List<Map> objList = pro.start();//获取返回对象
            if (0 == objList.size()) {
                isRunning = false;
                Log.logger.info("the dev data is null,end Timer");
                return;
            }
            JSONObject jobj = new JSONObject();
            try {
                jobj.put("projectorList", objList);
                String JsonStr = jobj.toString();

                HttpSend httpsend = new HttpSend(MainConfig.m_idealUrl);
                Log.logger.info("send data to idea cloud");
                httpsend.sendMessage(JsonStr);
            } catch (Exception ex) {
                Log.logger.error(ex.getMessage());
            } finally {
                isRunning = false;
                Log.logger.info("end Timer");
            }

        } else {
            Log.logger.info("Timer is runing");
        }

    }
}
